dojo.provide("uws.drawing.sqlerd.IdentifyingRelationship");
dojo.require('uws.drawing.Connection');

dojo.declare('uws.drawing.sqlerd.IdentifyingRelationship', uws.drawing.Connection,{
	constructor: function(id, surface, shape1, shape1_dir, shape2, shape2_dir, stereotype, custom, cardinality1, cardinality2){
		this.umltype = "iRelationship";
		
		//line
		this.shapeCon = this.group.createLine({
			x1:this.x1,
			y1:this.y1,
			x2:this.x2,
			y2:this.y2
		}).setStroke({color: "black",width:2});
		
		var custom = parseCustomAttribute(custom);
		
		//participation 1
		if(custom["totalParticipation1"][2]=="true"){
			var temp;
			
			if(this.x2>this.x1){
				temp = this.x1 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
				this.shape8Con = this.group.createLine({
					x1:this.x1,
					y1:this.y1 - 5,
					x2: temp,
					y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
				}).setStroke({color: "black",width:2});
			}else{
				temp = this.x1 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
				this.shape8Con = this.group.createLine({
					x1:this.x2,
					y1:this.y2 - 5,
					x2: temp,
					y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
				}).setStroke({color: "black",width:2});
			}
		}else{
			this.shape8Con = null;
		}
		
		//participation 2
		if(custom["totalParticipation2"][2]=="true"){
			var temp;
			
			if(this.x2>this.x1){
				temp = this.x2 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
				this.shape9Con = this.group.createLine({
					x1: this.x2,
					y1: this.y2 - 5,
					x2: temp,
					y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
				}).setStroke({color: "black",width:2});
				this.shape8Con.moveToBack();
			}else{
				temp = this.x2 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
				this.shape9Con = this.group.createLine({
					x1:this.x1,
					y1:this.y1 - 5,
					x2: temp,
					y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
				}).setStroke({color: "black",width:2});
			}
		}else{
			this.shape9Con = null;
		}
		
		//diamond1
		this.shape2Con = this.group.createRect({
			x:(((this.x2+this.x1)/2)-15),
			y:(((this.y2+this.y1)/2)-15),
			width:30,
			height:30
		}).setFill("white").setStroke("black").setTransform([dojox.gfx.matrix.rotateAt(40, {x: ((this.x2+this.x1)/2), y: ((this.y2+this.y1)/2)})]);
		
		//diamond2
		this.shape3Con = this.group.createRect({
			x:(((this.x2+this.x1)/2)-12),
			y:(((this.y2+this.y1)/2)-12),
			width:24,
			height:24
		}).setFill("white").setStroke("black").setTransform([dojox.gfx.matrix.rotateAt(40, {x: ((this.x2+this.x1)/2), y: ((this.y2+this.y1)/2)})]);
		
		this.stereotype_shape = this.group.createText({
			text: stereotype,
			x:((this.x2+this.x1)/2),
			y:(((this.y2+this.y1)/2)+30),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		var slope = this._getSlope();
		
		//cardinality1
		var x3;
		if(this.x2>this.x1){
			x3= this.x1 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}else{
			x3= this.x1 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}
		this.shape4Con = this.group.createText({
			text: cardinality1,
			x: x3,
			y: this._getY(this.x1, this.y1 + 10, x3, slope),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		//cardinality2
		if(this.x2>this.x1){
			x3 = this.x2 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}else{
			x3 = this.x2 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}
		this.shape5Con = this.group.createText({
			text: cardinality2,
			x: x3,
			y: this._getY(this.x2, this.y2 + 10, x3, slope),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		//role1
		if(this.x2>this.x1){
			x3= this.x1 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}else{
			x3= this.x1 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}
		this.shape6Con = this.group.createText({
			text: custom["role1"][2],
			x: x3,
			y: this._getY(this.x1, this.y1 + 10, x3, this._getSlope()),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		//role2
		if(this.x2>this.x1){
			x3 = this.x2 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}else{
			x3 = this.x2 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
		}
		this.shape7Con = this.group.createText({
			text: custom["role2"][2],
			x: x3,
			y: this._getY(this.x2, this.y2 + 10, x3, this._getSlope()),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		this.shape1_monitor = dojo.connect(shape1.shape_moveable,"onMoving",this,function(mover,shift){
			if (mover.shape.matrix == null) {
				mover.shape.matrix = {
					dx: 0,
					dy: 0
				};
			}
			
			this.shapeCon.setShape({
				x1:(this.con_x1 + mover.shape.matrix.dx),
				y1:(this.con_y1 + mover.shape.matrix.dy),
				x2:this.x2,
				y2:this.y2
			}).setStroke({color: "black",width:2});
			
			this.x1=(this.con_x1 + mover.shape.matrix.dx);
			this.y1=(this.con_y1 + mover.shape.matrix.dy);
			this.shapeCon.moveToBack();
			
			//participation 1
			if(this.shape8Con){
				var temp;
				
				if(this.x2>this.x1){
					temp = this.x1 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape8Con.setShape({
						x1:this.x1,
						y1:this.y1 - 5,
						x2: temp,
						y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
				}else{
					temp = this.x1 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape8Con.setShape({
						x1:this.x2,
						y1:this.y2 - 5,
						x2: temp,
						y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
				}
				this.shape8Con.moveToBack();
			}
			
			//participation 2
			if(this.shape9Con){
				var temp;
				
				if(this.x2>this.x1){
					temp = this.x2 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape9Con.setShape({
						x1: this.x2,
						y1: this.y2 - 5,
						x2: temp,
						y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
					
				}else{
					temp = this.x2 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape9Con.setShape({
						x1:this.x1,
						y1:this.y1 - 5,
						x2: temp,
						y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
				}
				this.shape9Con.moveToBack();
			}
			
			//diamond
			this.shape2Con.setShape({
				x:(((this.x2+this.x1)/2)-15),
				y:(((this.y2+this.y1)/2)-15),
				width:30,
				height:30
			}).setFill("white").setStroke("black").setTransform([dojox.gfx.matrix.rotateAt(40, {x: ((this.x2+this.x1)/2), y: ((this.y2+this.y1)/2)})]);
			
			//diamond
			this.shape3Con.setShape({
				x:(((this.x2+this.x1)/2)-12),
				y:(((this.y2+this.y1)/2)-12),
				width:24,
				height:24
			}).setFill("white").setStroke("black").setTransform([dojox.gfx.matrix.rotateAt(40, {x: ((this.x2+this.x1)/2), y: ((this.y2+this.y1)/2)})]);
			
			this.stereotype_shape.setShape({
				x:((this.x2+this.x1)/2),
				y:(((this.y2+this.y1)/2)+30)
			});
			
			//cardinality1
			var x3;
			if(this.x2>this.x1){
				x3= this.x1 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}else{
				x3= this.x1 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}
			this.shape4Con.setShape({
				x: x3,
				y: this._getY(this.x1, this.y1 + 10, x3, slope),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
			//cardinality2
			if(this.x2>this.x1){
				x3 = this.x2 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}else{
				x3 = this.x2 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}
			this.shape5Con.setShape({
				x: x3,
				y: this._getY(this.x2, this.y2 + 10, x3, slope),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
			//role1
			x3 = this.x1 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			this.shape6Con.setShape({
				x: x3,
				y: this._getY(this.x1, this.y1 + 10, x3, this._getSlope()),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
			//role2
			x3 = this.x2 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			this.shape7Con.setShape({
				x: x3,
				y: this._getY(this.x2, this.y2 + 10, x3, this._getSlope()),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
		});
		
		this.shape2_monitor = dojo.connect(shape2.shape_moveable,"onMoving",this,function(mover,shift){
			if (mover.shape.matrix == null) {
				mover.shape.matrix = {
					dx: 0,
					dy: 0
				};
			}
			
			this.shapeCon.setShape({
				x1:this.x1,
				y1:this.y1,
				x2:(this.con_x2 + mover.shape.matrix.dx),
				y2:(this.con_y2 + mover.shape.matrix.dy)
			}).setStroke({color: "black",width:2});
			
			this.x2=(this.con_x2 + mover.shape.matrix.dx);
			this.y2=(this.con_y2 + mover.shape.matrix.dy);
			this.shapeCon.moveToBack();
			
			//participation 1
			if(this.shape8Con){
				var temp;
				
				if(this.x2>this.x1){
					temp = this.x1 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape8Con.setShape({
						x1:this.x1,
						y1:this.y1 - 5,
						x2: temp,
						y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
				}else{
					temp = this.x1 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape8Con.setShape({
						x1:this.x2,
						y1:this.y2 - 5,
						x2: temp,
						y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
				}
				this.shape8Con.moveToBack();
			}
			
			//participation 2
			if(this.shape9Con){
				var temp;
				
				if(this.x2>this.x1){
					temp = this.x2 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape9Con.setShape({
						x1: this.x2,
						y1: this.y2 - 5,
						x2: temp,
						y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
					
				}else{
					temp = this.x2 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
					this.shape9Con.setShape({
						x1:this.x1,
						y1:this.y1 - 5,
						x2: temp,
						y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
					}).setStroke({color: "black",width:2});
				}
				this.shape9Con.moveToBack();
			}
			
			//diamond
			this.shape2Con.setShape({
				x:(((this.x2+this.x1)/2)-15),
				y:(((this.y2+this.y1)/2)-15),
				width:30,
				height:30
			}).setFill("white").setStroke("black").setTransform([dojox.gfx.matrix.rotateAt(40, {x: ((this.x2+this.x1)/2), y: ((this.y2+this.y1)/2)})]);
			
			//diamond
			this.shape3Con.setShape({
				x:(((this.x2+this.x1)/2)-12),
				y:(((this.y2+this.y1)/2)-12),
				width:24,
				height:24
			}).setFill("white").setStroke("black").setTransform([dojox.gfx.matrix.rotateAt(40, {x: ((this.x2+this.x1)/2), y: ((this.y2+this.y1)/2)})]);
			
			this.stereotype_shape.setShape({
				x:((this.x2+this.x1)/2),
				y:(((this.y2+this.y1)/2)+30)
			});
			
			var slope = this._getSlope();
		
			//cardinality1
			var x3;
			if(this.x2>this.x1){
				x3= this.x1 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}else{
				x3= this.x1 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}
			this.shape4Con.setShape({
				x: x3,
				y: this._getY(this.x1, this.y1 + 10, x3, slope),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
			//cardinality2
			if(this.x2>this.x1){
				x3 = this.x2 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}else{
				x3 = this.x2 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			}
			this.shape5Con.setShape({
				x: x3,
				y: this._getY(this.x2, this.y2 + 10, x3, slope),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
			//role1
			x3 = this.x1 + Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			this.shape6Con.setShape({
				x: x3,
				y: this._getY(this.x1, this.y1 + 10, x3, this._getSlope()),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
			
			//role2
			x3 = this.x2 - Math.round(this._getDistance(this.x1, this.y1, this.x2, this.y2)/4);
			this.shape7Con.setShape({
				x: x3,
				y: this._getY(this.x2, this.y2 + 10, x3, this._getSlope()),
				align:"middle"
			}).setFill("black").setFont(defaultFont);
		});
	},
	applyProperties:function(properties){
		var custom = parseCustomAttribute(properties.custom);
		
		if(this.shape8Con){
			this.shape8Con.removeShape();
			this.shape8Con = null;
		}
		
		if(custom["totalParticipation1"][2]=="true"){
			var temp = this.x1 + (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
			this.shape8Con = this.group.createLine({
				x1:this.x1,
				y1:this.y1 - 5,
				x2: temp,
				y2: this._getY(this.x1, this.y1 - 5, temp, this._getSlope())
			}).setStroke({color: "black",width:2});
		}
		
		if(this.shape9Con){
			this.shape9Con.removeShape();
			this.shape9Con = null;
		}
		
		if(custom["totalParticipation2"][2]=="true"){
			var temp = this.x2 - (this._getDistance(this.x1, this.y1, this.x2, this.y2)/2);
			this.shape9Con = this.group.createLine({
				x1:this.x2,
				y1:this.y2 - 5,
				x2: temp,
				y2: this._getY(this.x2, this.y2 - 5, temp, this._getSlope())
			}).setStroke({color: "black",width:2});
		}
		
		this.stereotype_shape.setShape({
			text: properties.stereotype,
			x:((this.x2+this.x1)/2),
			y:(((this.y2+this.y1)/2)+30),
			align:"middle"
		}).setFill("black").setFont(defaultFont);
		
		//cardinality1
		this.shape4Con.setShape({
			text: properties.cardinality1
		}).setFill("black").setFont(defaultFont);
		
		//cardinality2
		this.shape5Con.setShape({
			text: properties.cardinality2
		}).setFill("black").setFont(defaultFont);
	},
	_getY:function(x1, y1, x, slope){
		return ((slope * (x - x1)) + y1);
	},
	_getX:function(x1,y1, y, slope){
		return ((y-y1)/slope) + x1;
	},
	_getSlope:function(){
		var rise,run;
		
		if((this.x2-this.x1)==0) run = 0.0001;
		else run = this.x2 - this.x1;
		
		rise = this.y2 - this.y1;
		
		return rise/run;
	},
	_getDistance:function(x1,y1,x2,y2){
		return Math.sqrt(Math.pow(x2 - x1, 2), Math.pow(y2 - y1, 2));
	}
});